﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using Ventrian.SimpleGallery.Entities;

namespace DNNRest
{
    public partial class Handler1 : PortalModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                var file = HttpContext.Current.Request.Files["photo"];
                var fileName = HttpContext.Current.Request["name"];
                if (file == null) return;
                
                SaveImage(file, fileName);
            }
            catch (Exception exc)
            {

                throw;
            }
        }

        private void SaveImage(HttpPostedFile file, string fileName)
        {
            var stream = file.InputStream;
            var bmp = new Bitmap(stream);

            var height = Convert.ToInt32(bmp.Height * .5);
            var width = Convert.ToInt32(bmp.Width * .5);

            var myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            var myThumbnail = bmp.GetThumbnailImage(height, width, myCallback, IntPtr.Zero);
            myThumbnail.Save(Server.MapPath("/Portals/0/Gallery/Album/1/") + "mob_" + file.FileName);
                
            SaveImageToSimpleGallery(file, height, width, fileName);
        }

        private static void SaveImageToSimpleGallery(HttpPostedFile file, int height, int width, string fileName)
        {
            var pc = new PhotoController();

            var photo = new PhotoInfo
                            {
                                AlbumID = 1,
                                AlbumName = "iPhone",
                                ApproverDisplayName = "Mobile",
                                ApproverFirstName = "Mobile",
                                ApproverID = 1,
                                ApproverLastName = "Uploads",
                                ApproverUserName = "Mobile User",
                                AuthorDisplayName = "Mobile User",
                                AuthorFirstName = "Mobile",
                                AuthorID = 1,
                                AuthorLastName = "User",
                                AuthorUserName = "Mobile User",
                                BatchID = "1",
                                DateApproved = DateTime.Now,
                                DateCreated = DateTime.Now,
                                DateUpdated = DateTime.Now,
                                Description = fileName,
                                FileName = "mob_" + file.FileName,
                                Height = Convert.ToInt32(height),
                                HomeDirectory = "/Portals/0/Gallery/Album/1/",
                                IsApproved = true,
                                IsDefault = true,
                                ModuleID = 381,
                                Name = fileName,
                                Tags = "Mobile",
                                Width = Convert.ToInt32(width)
                            };

            pc.Add(photo);
        }

        public bool ThumbnailCallback()
        {
            return false;
        }
    }

}